home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-03  |  10.5 KB  |  476 lines

  1. /* install - copy files and set attributes
  2.    Copyright (C) 1989-1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Copy files and set their permission modes and, if possible,
  19.    their owner and group.  Used similarly to `cp'; typically
  20.    used in Makefiles to copy programs into their destination
  21.    directories.  It can also be used to create the destination
  22.    directories and any leading directories, and to set the final
  23.    directory's modes.  It refuses to copy files onto themselves.
  24.  
  25.    Options:
  26.    -g, +group=GROUP
  27.     Set the group ownership of the installed file or directory
  28.     to the group ID of GROUP (default is process's current
  29.     group).  GROUP may also be a numeric group ID.
  30.  
  31.    -m, +mode=MODE
  32.     Set the permission mode for the installed file or directory
  33.     to MODE, which is an octal number (default is 0755).
  34.  
  35.    -o, +owner=OWNER
  36.     If run as root, set the ownership of the installed file to
  37.     the user ID of OWNER (default is root).  OWNER may also be
  38.     a numeric user ID.
  39.  
  40.    -c    No effect.  For compatibility with old Unix versions of install.
  41.  
  42.    -s, +strip
  43.     Strip the symbol tables from installed files.
  44.  
  45.    -d, +directory
  46.     Create a directory and its leading directories, if they
  47.     do not already exist.  Set the owner, group and mode
  48.     as given on the command line.  Any leading directories
  49.     that are created are also given those attributes.
  50.     This is different from the SunOs 4.0 install, which gives
  51.     directories that it creates the default attributes.
  52.  
  53.    David MacKenzie <djm@ai.mit.edu> */
  54.  
  55. #include <stdio.h>
  56. #include <getopt.h>
  57. #include <ctype.h>
  58. #include <sys/types.h>
  59. #include <pwd.h>
  60. #include <grp.h>
  61. #include "system.h"
  62.  
  63. #ifdef POSIX
  64. #include <sys/wait.h>
  65. #else
  66. struct passwd *getpwnam ();
  67. struct group *getgrnam ();
  68. uid_t getuid ();
  69. gid_t getgid ();
  70. int wait ();
  71. void endpwent ();
  72. void endgrent ();
  73. #endif
  74.  
  75. /* True if C is an ASCII octal digit. */
  76. #define isodigit(c) ((c) >= '0' && c <= '7')
  77.  
  78. /* Number of bytes of a file to copy at a time. */
  79. #define READ_SIZE (32 * 1024)
  80.  
  81. char *basename ();
  82. char *xmalloc ();
  83. int atoo ();
  84. int change_attributes ();
  85. int copy_file ();
  86. int install_dir ();
  87. int install_file_in_dir ();
  88. int install_file_in_file ();
  89. int isdir ();
  90. int make_path ();
  91. int isnumber ();
  92. void error ();
  93. void get_ids ();
  94. void strip ();
  95. void strip_trailing_slashes ();
  96. void usage ();
  97.  
  98. /* The name this program was run with, for error messages. */
  99. char *program_name;
  100.  
  101. /* The user name that will own the files, or NULL to make the owner
  102.    the current user ID. */
  103. char *owner_name;
  104.  
  105. /* The user ID corresponding to `owner_name'. */
  106. uid_t owner_id;
  107.  
  108. /* The group name that will own the files, or NULL to make the group
  109.    the current group ID. */
  110. char *group_name;
  111.  
  112. /* The group ID corresponding to `group_name'. */
  113. gid_t group_id;
  114.  
  115. /* The permissions to which the files will be set.  The umask has
  116.    no effect. */
  117. int mode;
  118.  
  119. /* If nonzero, strip executable files after copying them. */
  120. int strip_files;
  121.  
  122. /* If nonzero, install a directory instead of a regular file. */
  123. int dir_arg;
  124.  
  125. struct option long_options[] =
  126. {
  127.   {"strip", 0, NULL, 's'},
  128.   {"directory", 0, NULL, 'd'},
  129.   {"group", 1, NULL, 'g'},
  130.   {"mode", 1, NULL, 'm'},
  131.   {"owner", 1, NULL, 'o'},
  132.   {NULL, 0, NULL, 0}
  133. };
  134.  
  135. void
  136. main (argc, argv)
  137.      int argc;
  138.      char **argv;
  139. {
  140.   int optc;
  141.   int errors = 0;
  142.  
  143.   program_name = argv[0];
  144.   owner_name = NULL;
  145.   group_name = NULL;
  146.   mode = 0755;
  147.   strip_files = 0;
  148.   dir_arg = 0;
  149.   umask (0);
  150.  
  151.   while ((optc = getopt_long (argc, argv, "csdg:m:o:", long_options, (int *) 0))
  152.      != EOF)
  153.     {
  154.       switch (optc)
  155.     {
  156.     case 'c':
  157.       break;
  158.     case 's':
  159.       strip_files = 1;
  160.       break;
  161.     case 'd':
  162.       dir_arg = 1;
  163.       break;
  164.     case 'g':
  165.       group_name = optarg;
  166.       break;
  167.     case 'm':
  168.       mode = atoo (optarg);
  169.       if (mode < 0 || mode > 07777)
  170.         error (1, 0, "invalid file mode `%s'", optarg);
  171.       break;
  172.     case 'o':
  173.       owner_name = optarg;
  174.       break;
  175.     default:
  176.       usage ();
  177.     }
  178.     }
  179.  
  180.   switch (argc - optind)
  181.     {
  182.     case 0:
  183.       usage ();
  184.       break;
  185.     case 1:
  186.       if (!dir_arg || strip_files)
  187.     usage ();
  188.       get_ids ();
  189.       strip_trailing_slashes (argv[optind]);
  190.       errors = make_path (argv[optind], mode, mode, owner_id, group_id, NULL);
  191.       break;
  192.     case 2:
  193.       if (dir_arg)
  194.     usage ();
  195.       get_ids ();
  196.       strip_trailing_slashes (argv[argc - 1]);
  197.       strip_trailing_slashes (argv[optind]);
  198.       if (!isdir (argv[argc - 1]))
  199.     errors = install_file_in_file (argv[optind], argv[argc - 1]);
  200.       else
  201.     errors = install_file_in_dir (argv[optind], argv[argc - 1]);
  202.       break;
  203.     default:
  204.       strip_trailing_slashes (argv[argc - 1]);
  205.       if (dir_arg || !isdir (argv[argc - 1]))
  206.     usage ();
  207.       get_ids ();
  208.       for (; optind < argc - 1; ++optind)
  209.     {
  210.       strip_trailing_slashes (argv[optind]);
  211.       errors |= install_file_in_dir (argv[optind], argv[argc - 1]);
  212.     }
  213.       break;
  214.     }
  215.  
  216.   exit (errors);
  217. }
  218.  
  219. /* Copy file FROM onto file TO and give TO the appropriate
  220.    attributes.
  221.    Return 0 if successful, 1 if an error occurs. */
  222.  
  223. int
  224. install_file_in_file (from, to)
  225.      char *from;
  226.      char *to;
  227. {
  228.   if (copy_file (from, to))
  229.     return 1;
  230.   if (strip_files)
  231.     strip (to);
  232.   return change_attributes (to);
  233. }
  234.  
  235. /* Copy file FROM into directory TO_DIR, keeping its same name,
  236.    and give the copy the appropriate attributes.
  237.    Return 0 if successful, 1 if not. */
  238.  
  239. int
  240. install_file_in_dir (from, to_dir)
  241.      char *from;
  242.      char *to_dir;
  243. {
  244.   char *from_base;
  245.   char *to;
  246.   int ret;
  247.  
  248.   from_base = basename (from);
  249.   to = xmalloc ((unsigned) (strlen (to_dir) + strlen (from_base) + 2));
  250.   sprintf (to, "%s/%s", to_dir, from_base);
  251.   ret = install_file_in_file (from, to);
  252.   free (to);
  253.   return ret;
  254. }
  255.  
  256. /* A chunk of a file being copied. */
  257. static char buffer[READ_SIZE];
  258.  
  259. /* Copy file FROM onto file TO, creating TO if necessary.
  260.    Return 0 if the copy is successful, 1 if not. */
  261.  
  262. int
  263. copy_file (from, to)
  264.      char *from;
  265.      char *to;
  266. {
  267.   int fromfd, tofd;
  268.   int bytes;
  269.   int ret = 0;
  270.   struct stat from_stats, to_stats;
  271.  
  272.   if (stat (from, &from_stats))
  273.     {
  274.       error (0, errno, "%s", from);
  275.       return 1;
  276.     }
  277.   if (!S_ISREG (from_stats.st_mode))
  278.     {
  279.       error (0, 0, "`%s' is not a regular file", from);
  280.       return 1;
  281.     }
  282.   if (stat (to, &to_stats) == 0)
  283.     {
  284.       if (!S_ISREG (to_stats.st_mode))
  285.     {
  286.       error (0, 0, "`%s' is not a regular file", to);
  287.       return 1;
  288.     }
  289.       if (from_stats.st_dev == to_stats.st_dev
  290.       && from_stats.st_ino == to_stats.st_ino)
  291.     {
  292.       error (0, 0, "`%s' and `%s' are the same file", from, to);
  293.       return 1;
  294.     }
  295.       /* If unlink fails, try to proceed anyway.  */
  296.       unlink (to);
  297.     }
  298.  
  299.   fromfd = open (from, O_RDONLY, 0);
  300.   if (fromfd == -1)
  301.     {
  302.       error (0, errno, "%s", from);
  303.       return 1;
  304.     }
  305.  
  306.   /* Make sure to open the file in a mode that allows writing. */
  307.   tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  308.   if (tofd == -1)
  309.     {
  310.       error (0, errno, "%s", to);
  311.       close (fromfd);
  312.       return 1;
  313.     }
  314.  
  315.   while ((bytes = read (fromfd, buffer, READ_SIZE)) > 0)
  316.     if (write (tofd, buffer, bytes) != bytes)
  317.       {
  318.     error (0, errno, "%s", to);
  319.     goto copy_error;
  320.       }
  321.  
  322.   if (bytes == -1)
  323.     {
  324.       error (0, errno, "%s", from);
  325.       goto copy_error;
  326.     }
  327.  
  328.   if (close (fromfd) < 0)
  329.     {
  330.       error (0, errno, "%s", from);
  331.       ret = 1;
  332.     }
  333.   if (close (tofd) < 0)
  334.     {
  335.       error (0, errno, "%s", to);
  336.       ret = 1;
  337.     }
  338.   return ret;
  339.  
  340.  copy_error:
  341.   close (fromfd);
  342.   close (tofd);
  343.   return 1;
  344. }
  345.  
  346. /* Set the attributes of file or directory PATH.
  347.    Return 0 if successful, 1 if not. */
  348.  
  349. int
  350. change_attributes (path)
  351.      char *path;
  352. {
  353.   if (chmod (path, mode) || chown (path, owner_id, group_id))
  354.     {
  355.       error (0, errno, "%s", path);
  356.       return 1;
  357.     }
  358.   return 0;
  359. }
  360.  
  361. /* Strip the symbol table from the file PATH.
  362.    We could dig the magic number out of the file first to
  363.    determine whether to strip it, but the header files and
  364.    magic numbers vary so much from system to system that making
  365.    it portable would be very difficult.  Not worth the effort. */
  366.  
  367. void
  368. strip (path)
  369.      char *path;
  370. {
  371.   int pid, status;
  372.  
  373.   pid = fork ();
  374.   switch (pid)
  375.     {
  376.     case -1:
  377.       error (1, errno, "cannot fork");
  378.       break;
  379.     case 0:            /* Child. */
  380.       execlp ("strip", "strip", path, (char *) NULL);
  381.       error (1, errno, "cannot run strip");
  382.       break;
  383.     default:            /* Parent. */
  384.       /* Parent process. */
  385.       while (pid != wait (&status))    /* Wait for kid to finish. */
  386.     /* Do nothing. */ ;
  387.       break;
  388.     }
  389. }
  390.  
  391. /* Initialize the user and group ownership of the files to install. */
  392.  
  393. void
  394. get_ids ()
  395. {
  396.   struct passwd *pw;
  397.   struct group *gr;
  398.  
  399.   if (owner_name)
  400.     {
  401.       pw = getpwnam (owner_name);
  402.       if (pw == NULL)
  403.     {
  404.       if (!isnumber (owner_name))
  405.         error (1, 0, "invalid user `%s'", owner_name);
  406.       owner_id = atoi (owner_name);
  407.     }
  408.       else
  409.     owner_id = pw->pw_uid;
  410.       endpwent ();
  411.     }
  412.   else
  413.     owner_id = getuid ();
  414.  
  415.   if (group_name)
  416.     {
  417.       gr = getgrnam (group_name);
  418.       if (gr == NULL)
  419.     {
  420.       if (!isnumber (group_name))
  421.         error (1, 0, "invalid group `%s'", group_name);
  422.       group_id = atoi (group_name);
  423.     }
  424.       else
  425.     group_id = gr->gr_gid;
  426.       endgrent ();
  427.     }
  428.   else
  429.     group_id = getgid ();
  430. }
  431.  
  432. /* Return nonzero if STR is an ASCII representation of a nonzero
  433.    decimal integer, zero if not. */
  434.  
  435. int
  436. isnumber (str)
  437.      char *str;
  438. {
  439.   if (*str == 0)
  440.     return 0;
  441.   for (; *str; str++)
  442.     if (!isdigit (*str))
  443.       return 0;
  444.   return 1;
  445. }
  446.  
  447. /* Return the value of the octal digit string STR.
  448.    Return -1 if STR does not represent a valid octal number. */
  449.  
  450. int
  451. atoo (str)
  452.      char *str;
  453. {
  454.   int num;
  455.  
  456.   if (*str == 0)
  457.     return -1;
  458.   for (num = 0; isodigit (*str); ++str)
  459.     num = num * 8 + *str - '0';
  460.   return *str ? -1 : num;
  461. }
  462.  
  463. void
  464. usage ()
  465. {
  466.    fprintf (stderr, "\
  467. Usage: %s [options] [-s] [+strip] source dest\n\
  468.        %s [options] [-s] [+strip] source... directory\n\
  469.        %s [options] {-d,+directory} directory\n\
  470. Options:\n\
  471.        [-c] [-g group] [-m mode] [-o owner]\n\
  472.        [+group=group] [+mode=mode] [+owner=owner]\n",
  473.         program_name, program_name, program_name);
  474.   exit (1);
  475. }
  476.